package com.test.mchelepesa.activity_fragment_rice.fragment_rice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseLoanFragment_rice;
import com.test.mchelepesa.activity_fragment_rice.ServiceFeedbackActivity_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.bean_rice.process_rice.ProcessInfo_rice;
import com.test.mchelepesa.dialog_rice.DeleteDataDialog_rice;
import com.test.mchelepesa.util_rice.helper_rice.UserInfoHelper_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;


public class LoanFailFragment_rice extends BaseLoanFragment_rice {

    private View deleteDataBtn;

    private Runnable onAttachRunnable = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_loan_fail_rice, container, false);
        init_rice(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init_rice(View view){

        TextView textTv = view.findViewById(R.id.fragment_loan_fail_tv_text);

        initLicense(
                view.findViewById(R.id.fragment_loan_fail_view_license),
                view.findViewById(R.id.fragment_loan_fail_license_logo),
                view.findViewById(R.id.fragment_loan_fail_license_company_name)
        );

        view.findViewById(R.id.fragment_loan_fail_btn_refresh_status).setOnClickListener(v -> HttpManager_rice.getInstance().requestUserProcess());
        String againApplyDate = ProcessInfo_rice.getInstance().getAgainApplyDate();

        if (!TextUtils.isEmpty(againApplyDate)) textTv.setText(getString(R.string.fragment_loan_fail_text_1)+ againApplyDate);
        else {
            String days = ProcessInfo_rice.getInstance().getDeniedBetweenDay();
            if (TextUtils.isEmpty(days)) days = AppShowInfo_rice.getInstance().getRejectPeriod();
            textTv.setText(getString(R.string.fragment_loan_fail_text_1)+ days + getString(R.string.fragment_loan_fail_text_2));
        }

        view.findViewById(R.id.fragment_loan_fail_btn_complaint).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("HOME_FAILED_COMPLAINT_CLICK");
            startActivity(new Intent(requireActivity(), ServiceFeedbackActivity_rice.class));
        });

        deleteDataBtn = view.findViewById(R.id.fragment_loan_fail_btn_delete_data);

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (onAttachRunnable!=null){
            onAttachRunnable.run();
            onAttachRunnable = null;
        }
    }

    public void onDropConfigResponse(){
        if (!isAdded()) {
            onAttachRunnable = this::onDropConfigResponse;
            return;
        }
        if (UserInfoHelper_rice.getInstance().showDropButton()){
            deleteDataBtn.setVisibility(View.VISIBLE);
            deleteDataBtn.setOnClickListener(v -> {
                HttpManager_rice.getInstance().saveUserBuriedPoint("HOME_FAILED_DELETE_DATA_CLICK");
                new DeleteDataDialog_rice(requireContext()).show();
            });
        }else deleteDataBtn.setVisibility(View.GONE);
    }

}